import mysql.connector

config = {'host': '127.0.0.1', 'database': 'denglibin', 'user': 'root',
          'password': 'denglibin', 'port': 3306, 'charset': 'utf8'}
conn = None
cursor = None
# 获取连接
try:
    # 包裹参数传递的实现是在定义函数时在形参前面加上*或**，*所对应的形参会被解释为一个元组（tuple)而**所对应的形参会被解释为一个字典
    conn = mysql.connector.connect(**config)
    cursor = conn.cursor()
    sql = 'select  id,  name from user where id= %s'
    params = (1,)
    cursor.execute(sql, params)
    # row 是一个 元组
    for row in cursor:
        print(row[0], row[1])
except mysql.connector.Error as e:
        print('connect failed:', e)
# 关闭
if cursor:
    cursor.close()
if conn:
    conn.close()
